package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.MdinfoDao;
import net.mysoft.budgetsoft.system.domain.TMdinfo;
import net.mysoft.budgetsoft.system.domain.comparator.MdinfoComparator;
import net.mysoft.budgetsoft.system.pagemodel.Mdinfo;
import net.mysoft.budgetsoft.system.service.MdinfoService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2014-04-24 17:59:03 
 */
@Service("mdinfoService")
public class MdinfoServiceImpl implements MdinfoService{
	@Resource
	private MdinfoDao mdinfoDao;
	
	public MdinfoDao getMdinfoDao(){
		return this.mdinfoDao;
	}
	
	public void setMdinfoDao(MdinfoDao mdinfoDao){
		this.mdinfoDao = mdinfoDao;
	}
	
	@Override
	public void add(Mdinfo mdinfo){
		TMdinfo t = new TMdinfo();
		BeanUtils.copyProperties(mdinfo, t);
		t.setC_mdcode(UUID.randomUUID().toString());
		mdinfoDao.save(t);
	}
	
	@Override
	public void edit(Mdinfo mdinfo){
		TMdinfo t = new TMdinfo();
		BeanUtils.copyProperties(mdinfo, t);
		mdinfoDao.update(t);
	}
	
	@Override
	public void delete(Mdinfo mdinfo) {
		for (String id : mdinfo.getIds().split(",")) {
			Object t =  mdinfoDao.get(TMdinfo.class, id);
			if (t != null) {
				 mdinfoDao.delete(t);
			}
		}
	}
	
	@Override
	public List<Mdinfo> find(Mdinfo mdinfo) {
		String hql = "from TMdinfo t where 1=1 ";
		List paramList = new ArrayList();
		hql = addWhere(mdinfo, hql, paramList);
		List<TMdinfo> toList = mdinfoDao.find(hql, paramList);
		Collections.sort(toList, new MdinfoComparator());
		List<Mdinfo> oList = new ArrayList<Mdinfo>();
		oList = changeModel(toList);
		return oList;
	}
	
	@Override
	public DataGrid datagrid(Mdinfo mdinfo){
		DataGrid dg = new DataGrid();
		String hql = "from TMdinfo t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(mdinfo, hql, params);
		hql = addOrder(mdinfo, hql);
		String totalHql = "select count(*) " + hql;
		List<TMdinfo> toList = mdinfoDao.find(hql, params, mdinfo.getPage(), mdinfo.getRows());
		List<Mdinfo> oList = new ArrayList<Mdinfo>();
		oList = changeModel(toList);
		dg.setTotal(mdinfoDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	private String addWhere(Mdinfo mdinfo, String hql, List<Object> params) {
		return hql;
	}
	
	private String addOrder(Mdinfo mdinfo, String hql) {
		if (mdinfo.getSort() != null) {
			hql += " order by " + mdinfo.getSort() + " " + mdinfo.getOrder();
		}
		return hql;
	}
	
	public List<Mdinfo> changeModel(List<TMdinfo> tMdinfo ){
		List<Mdinfo> list = new ArrayList<Mdinfo>();
		for(TMdinfo to:tMdinfo){
			Mdinfo o = new Mdinfo();
			BeanUtils.copyProperties(to, o);
			//如果有主键长度为6（010101）就认为该菜单节点为子节点
			o.setIdLeaf(to.getC_mdcode()!= null && to.getC_mdcode().length()==6 ? "1" : "0");
			list.add(o);
			System.out.println(o.getC_mdcode()+","+o.getC_cname()+","+o.getIdLeaf());
		}
		return list;
	}
}
